package cn.gzsxt.mapper.provider;

import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class AdminProvider {
	
	private static final Logger LOGGER =LogManager.getLogger(AdminProvider.class);
	
	/**
	 * 通过条件判断，返回对应的记录数
	 * @param condition
	 * @return
	 */
	public String count(Map<String, Object> condition){
		//第一步：编写公用的SQL语句
		String sql="SELECT count(*) FROM tb_basic_admin WHERE 1=1";
		//第二步：创建字符串构建对象
		StringBuilder builder=new StringBuilder(sql);
		//第三步：拼接动态SQL语句
		if (condition.get("ADMIN_ID")!=null) {
			builder.append(" AND ADMIN_ID=#{ADMIN_ID}");
		}
		if (condition.get("ADMIN_NAME")!=null) {
			builder.append(" AND ADMIN_NAME like CONCAT('%', #{ADMIN_NAME},'%')");
		}
		
		if (condition.get("ADMIN_ACCOUNT")!=null) {
			builder.append(" AND ADMIN_ACCOUNT like CONCAT('%', #{ADMIN_ACCOUNT},'%')");
		}
		
		if (condition.get("ADMIN_PWD")!=null) {
			builder.append(" AND ADMIN_PWD=#{ADMIN_PWD}");
		}
		
		if (condition.get("ADMIN_STATUS")!=null) {
			builder.append(" AND ADMIN_STATUS=#{ADMIN_STATUS}");
		}
		
		if (condition.get("ADMIN_CREATE_DATE")!=null) {
			builder.append(" AND ADMIN_CREATE_DATE=#{ADMIN_CREATE_DATE}");
		}
		
		if (condition.get("ROLE_ID")!=null) {
			builder.append(" AND ROLE_ID=#{ROLE_ID}");
		}
	
		LOGGER.debug("AdminProvider.count:SQL语句："+builder.toString());
		
		//第四步：返回SQL语句
		return builder.toString();
	}
	

	
	public String deleteByIds(Map<String,Object[] > idsMap){
		//第一步：编写公共SQL
		String sql="DELETE FROM tb_basic_admin WHERE ADMIN_ID in ";
		//第二步：字符串构建对象
		StringBuilder builder=new StringBuilder(sql);
		//第三步：拼接SQL语句
		Object[] ids = idsMap.get("array");
		builder.append("(");
		for(int i=0;i<ids.length;i++){
			//Mybatis，数据元素可以使用#{array[下标]}获得对应的值
			builder.append("#{array["+i+"]},");
		}
		//删除一个逗号
		builder.delete(builder.length()-1, builder.length());
		builder.append(")");
		
		LOGGER.debug("AdminProvider.deleteByIds:批量删除SQL语句："+builder.toString());
		//第四步：返回SQL
		return builder.toString();
	}
	
	/**
	 * 根据条件查询并且分页
	 * @param condition
	 * @param start
	 * @param size
	 * @return
	 */
	public String findByConditionAndPage  (@Param("condition") Map<String, Object> condition, @Param("start") int start, @Param("size") int size){
		
		
		//第一步：编写公用的SQL语句
		String sql="SELECT * FROM tb_basic_admin WHERE 1=1";
		//第二步：创建字符串构建对象
		StringBuilder builder=new StringBuilder(sql);
		//第三步：拼接动态SQL语句
		if (condition.get("ADMIN_ID")!=null) {
			builder.append(" AND ADMIN_ID=#{condition.ADMIN_ID}");
		}
		if (condition.get("ADMIN_NAME")!=null) {
			builder.append(" AND ADMIN_NAME like CONCAT('%', #{condition.ADMIN_NAME},'%')");
		}
		
		if (condition.get("ADMIN_ACCOUNT")!=null) {
			builder.append(" AND ADMIN_ACCOUNT like CONCAT('%', #{condition.ADMIN_ACCOUNT},'%')");
		}
		
		if (condition.get("ADMIN_PWD")!=null) {
			builder.append(" AND ADMIN_PWD=#{condition.ADMIN_PWD}");
		}
		
		if (condition.get("ADMIN_STATUS")!=null) {
			builder.append(" AND ADMIN_STATUS=#{condition.ADMIN_STATUS}");
		}
		
		if (condition.get("ADMIN_CREATE_DATE")!=null) {
			builder.append(" AND ADMIN_CREATE_DATE=#{condition.ADMIN_CREATE_DATE}");
		}
		
		if (condition.get("ROLE_ID")!=null) {
			builder.append(" AND ROLE_ID=#{condition.ROLE_ID}");
		}
	
		
		builder.append(" LIMIT #{start},#{size}");
		
		LOGGER.debug("AdminProvider.findByConditionAndPage:SQL语句："+builder.toString());
		
		//第四步：返回SQL语句
		return builder.toString();
		
	}
	
	/**
	 * 根据条件拼接动态的更新语句,拼接非空字段
	 * @param admin
	 * @return
	 */
	public String updateForNotnull(Map<String, Object> admin){
		//第一步：声明一个公用的SQL
		 String sql="UPDATE tb_basic_admin SET ";
		//第二步：创建一个字符串构建对象
		 StringBuilder builder=new StringBuilder(sql);
		//第三步：构建SQL语句
		 if (admin.get("ADMIN_NAME")!=null) {
			 builder.append(" ADMIN_NAME=#{ADMIN_NAME},");
		 }
		 if (admin.get("ADMIN_ACCOUNT")!=null) {
			 builder.append(" ADMIN_ACCOUNT=#{ADMIN_ACCOUNT},");
		 }
		 if (admin.get("ADMIN_PWD")!=null) {
			 builder.append(" ADMIN_PWD=#{ADMIN_PWD},");
		 }
		 if (admin.get("ADMIN_STATUS")!=null) {
			 builder.append(" ADMIN_STATUS=#{ADMIN_STATUS},");
		 }
		 if (admin.get("ADMIN_CREATE_DATE")!=null) {
			 builder.append(" ADMIN_CREATE_DATE=#{ADMIN_CREATE_DATE},");
		 }
		 if (admin.get("ROLE_ID")!=null) {
			 builder.append(" ROLE_ID=#{ROLE_ID},");
		 }
		 //删除最后一个逗号
		 builder.delete(builder.length()-1, builder.length());
		 //加上更新的条件
		 builder.append(" WHERE ADMIN_ID=#{ADMIN_ID}");
		 
			
		 LOGGER.debug("管理员更新动态SQL语句："+builder.toString());
		 
		//第四步：返回SQL语句
		 return builder.toString();
	}


}
